package com.dongdong.service.impl;

import com.dongdong.mapper.TbItemMapper;
import com.dongdong.model.TbItem;
import com.dongdong.service.ItemService;
import com.dongdong.vo.ItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper itemMapper;

    @Override
    public TbItem getItemById(Long id) {
        return itemMapper.selectByPrimaryKey(id);
    }

    @Override
    public void subCount(List<ItemVO> itemVOS) {
//        尽量不要就在for循环里面操作数据库
//        实在没办法是时候  才可以
//        不能批量修改  所以要在  for循环里面操作
        for (ItemVO itemVO : itemVOS) {
            TbItem tbItem = itemMapper.selectByPrimaryKey(itemVO.getId());
            itemVO.setStockCount(tbItem.getStockCount()-itemVO.getStockCount());
            TbItem item = new TbItem();
            BeanUtils.copyProperties(itemVO,item);
            itemMapper.updateByPrimaryKeySelective(item);
        }
    }
}
